<?php

namespace App\Jobs;

use App\Models\SiteGold;
use App\Models\UserGold;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class VerifyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $param;

    // 各种审核操作（百度审核）
    // TODO 已经取消（在控制器中已经经过了百度审核，所以这里不用在做审核操作了）

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($param = '')
    {
        $this->param = $param;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $_id   = $this->param['id'] ?? 0;
        $model = $this->param['model'] ?? '';
        $field = $this->param['field'] ?? 'content'; // 需要审核的字段
        // 参数不齐全，不能执行
        if(empty($_id) || empty($model)){
            return $this->delete();
        }

        // return $this->release(10 * $attempts);
        // 避免过大，一直占用
        $attempts = $this->attempts();
        if($attempts > 50){
            return $this->delete();
        }

        // 获取数据
        $model = '\\App\\Models\\'.$model;
        $doDb  = new $model();
        $info  = $doDb->isCache(true)->oneFind(['id' => $_id]);
        // 如果数据不存在
        if(empty($info)){
            return $this->delete();
        }
        if($info['verify'] != 0){
            return $this->delete();
        }

        // 执行审核任务
        $content = $info[$field];
        try{
            // 判断任务是否完成（如果任务完成）
            /*$res = $uGoldDb->isFinish($taskInfo, $uid);
            if($res){
                return $this->delete();
            }*/

            // 执行操作 如果成功：审核通过
            $result = $doDb->where('id',$_id)->update(['verify' => 1]);

        } catch(\Exception $e){
            // 如果执行失败
            Log::info('队列【'.__CLASS__.'】:执行异常：'.$doDb->getError());

            return $this->release(10 * $attempts);
        }

        // 删除缓存
        $doDb->deleteCacheShow($info);

        return $this->delete();
    }
}
